package com.sweets.look.ui.widget.supertoast;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;

import java.util.Iterator;
import java.util.LinkedList;

class ManagerSuperActivityToast extends Handler {
    final class Messages {
        private static final int DISPLAY = 1146306900;
        private static final int REMOVE = 1381187924;

        private Messages() {
            super();
        }
    }

    private final LinkedList mList;
    private static ManagerSuperActivityToast mManagerSuperActivityToast;

    private ManagerSuperActivityToast() {
        super();
        this.mList = new LinkedList();
    }


    void add(SuperActivityToast superActivityToast) {
        this.mList.add(superActivityToast);
        this.showNextSuperToast();
    }

    void cancelAllSuperActivityToasts() {
        this.removeMessages(1146306900);
        this.removeMessages(1381187924);
        Iterator v1 = this.mList.iterator();
        while (v1.hasNext()) {
            Object v0 = v1.next();
            if (!((SuperActivityToast) v0).isShowing()) {
                continue;
            }

            if (((SuperActivityToast) v0).getDismissListerner() != null) {
                ((SuperActivityToast) v0).getDismissListerner().onClick(((SuperActivityToast) v0).getView());
            }

            ((SuperActivityToast) v0).getViewGroup().removeView(((SuperActivityToast) v0).getView());
            ((SuperActivityToast) v0).getViewGroup().invalidate();
        }

        this.mList.clear();
    }

    void cancelAllSuperActivityToastsForActivity(Activity activity) {
        Iterator v1 = this.mList.iterator();
        while (v1.hasNext()) {
            Object v0 = v1.next();
            if (((SuperActivityToast) v0).getActivity() == null) {
                continue;
            }

            if (!((SuperActivityToast) v0).getActivity().equals(activity)) {
                continue;
            }

            if (((SuperActivityToast) v0).isShowing()) {
                if (((SuperActivityToast) v0).getDismissListerner() != null) {
                    ((SuperActivityToast) v0).getDismissListerner().onClick(((SuperActivityToast) v0).
                            getView());
                }

                ((SuperActivityToast) v0).getViewGroup().removeView(((SuperActivityToast) v0).getView());
            }

            this.removeMessages(1146306900, v0);
            this.removeMessages(1381187924, v0);
            v1.remove();
        }
    }

    private void displaySuperToast(SuperActivityToast superActivityToast) {
        if (!superActivityToast.isShowing()) {
            ViewGroup v3 = superActivityToast.getViewGroup();
            View v2 = superActivityToast.getView();
            if (v3 != null) {
                try {
                    v3.addView(v2);
                    if (superActivityToast.getShowImmediate()) {
                    } else {
                        v2.startAnimation(this.getShowAnimation());
                    }
                } catch (IllegalStateException v0) {
                    this.cancelAllSuperActivityToastsForActivity(superActivityToast.getActivity());
                }
            }

            if (superActivityToast.isIndeterminate()) {
                return;
            }

            Message v1 = this.obtainMessage(1381187924);
            v1.obj = superActivityToast;
            this.sendMessageDelayed(v1, (((long) superActivityToast.getDuration())) + this.getShowAnimation()
                    .getDuration());
        }
    }

    private Animation getDismissAnimation() {
        TranslateAnimation v0 = new TranslateAnimation(1, 0f, 1, 0f, 1, 0f, 1, 0.1f);
        AlphaAnimation v9 = new AlphaAnimation(1f, 0f);
        AnimationSet v10 = new AnimationSet(true);
        v10.addAnimation(((Animation) v0));
        v10.addAnimation(((Animation) v9));
        v10.setInterpolator(new DecelerateInterpolator());
        v10.setDuration(250);
        return ((Animation) v10);
    }

    static ManagerSuperActivityToast getInstance() {
        ManagerSuperActivityToast v0_1;
        Class v1 = ManagerSuperActivityToast.class;
        synchronized (v1) {
            if (ManagerSuperActivityToast.mManagerSuperActivityToast != null) {
                v0_1 = ManagerSuperActivityToast.mManagerSuperActivityToast;
            } else {
                ManagerSuperActivityToast.mManagerSuperActivityToast = new ManagerSuperActivityToast();
                v0_1 = ManagerSuperActivityToast.mManagerSuperActivityToast;
            }
        }
        return v0_1;
    }

    private Animation getShowAnimation() {
        TranslateAnimation v0 = new TranslateAnimation(1, 0f, 1, 0f, 1, 0.1f, 1, 0f);
        AlphaAnimation v9 = new AlphaAnimation(0f, 1f);
        AnimationSet v10 = new AnimationSet(true);
        v10.addAnimation(((Animation) v0));
        v10.addAnimation(((Animation) v9));
        v10.setInterpolator(new DecelerateInterpolator());
        v10.setDuration(250);
        return ((Animation) v10);
    }

    public void handleMessage(Message message) {
        Object v0 = message.obj;
        switch (message.what) {
            case 1146306900: {
                this.displaySuperToast(((SuperActivityToast) v0));
                break;
            }
            case 1381187924: {
                this.removeSuperToast(((SuperActivityToast) v0));
                break;
            }
            default: {
                super.handleMessage(message);
                break;
            }
        }
    }

    void removeSuperToast(final SuperActivityToast superActivityToast) {
        View v1;
        ViewGroup v2;
        if (!superActivityToast.isShowing()) {
            this.mList.remove(superActivityToast);
            v2 = superActivityToast.getViewGroup();
            v1 = superActivityToast.getView();
            if (v2 == null) {
                return;
            }

            if (v1 == null) {
                return;
            }

            v2.removeView(v1);
            this.mList.poll();
            return;
        }

        this.removeMessages(1381187924, superActivityToast);
        v2 = superActivityToast.getViewGroup();
        v1 = superActivityToast.getView();
        if (v2 != null) {
            Animation v0 = this.getDismissAnimation();
            v0.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationEnd(Animation animation) {
                    if (!superActivityToast.getTouchToDismiss() && superActivityToast
                            .getDismissListerner() != null) {
                        superActivityToast.getDismissListerner().onClick(superActivityToast
                                .getView());
                    }

                    ManagerSuperActivityToast.this.showNextSuperToast();
                }

                public void onAnimationRepeat(Animation animation) {
                }

                public void onAnimationStart(Animation animation) {
                }
            });
            v1.startAnimation(v0);
            v2.removeView(v1);
            this.mList.poll();
        }
    }

    private void showNextSuperToast() {
        Object v1 = this.mList.peek();
        if (!this.mList.isEmpty() && (((SuperActivityToast) v1).getActivity() != null && !((SuperActivityToast)
                v1).isShowing())) {
            Message v0 = this.obtainMessage(1146306900);
            v0.obj = v1;
            this.sendMessage(v0);
        }
    }
}

